package com.cainiao.service.deptserviceimpl;

import com.cainiao.mapper.StudentMapper;
import com.cainiao.pojo.ClassStudentCountData;
import com.cainiao.pojo.PageBean;
import com.cainiao.pojo.Student;
import com.cainiao.pojo.StudentQueryParam;
import com.cainiao.service.StudentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageBean findStudent(StudentQueryParam studentQueryParam) {
        PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());
        List<Student> student = studentMapper.findStudent(studentQueryParam);
        Page<Student> page=(Page<Student>) student;
        return new PageBean(page.getTotal(),page.getResult());
    }

    @Override
    public void delete(List<Long> ids) {
        studentMapper.delete(ids);
    }

    @Override
    public void add(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.add(student);
    }

    @Override
    public Student findById(Integer id) {
        return studentMapper.findById(id);

    }

    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    @Override
    public void updateViolation(Integer id, Integer score) {
        studentMapper.updateViolation(id,score);
    }

    @Override
    public List<Map> studentDegreeData() {
        return studentMapper.studentDegreeData();
    }

    @Override
    public ClassStudentCountData findStudentCountData() {
        List<Map> maps = studentMapper.studentCountData();
        List<Object> name = maps.stream().map(map -> map.get("name")).toList();
        List<Object> count = maps.stream().map(map -> map.get("count")).toList();
        return new ClassStudentCountData(name,count);
    }
}
